﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using XcoAppSpaces.Transport.MSMQ;
using XcoAppSpaces.Contracts.Transport;
using System.Threading;
using test.XcoAppSpaces.Transport.MSMQ.TestLogger;
using XcoAppSpaces.Contracts.Logging;
using XcoAppSpaces.Serialization.Binary;

namespace test.XcoAppSpaces.Transport.MSMQ
{
    [TestFixture]
    public class PerformanceTest
    {
        private DateTime startedAt;
        private XcoBinarySerializer serializer = new XcoBinarySerializer();

        private const string msmqName1 = @".\private$\xvsmMsmq1";
        private const string msmqName2 = @".\private$\xvsmMsmq2";
        XcoMSMQTransportService comm1;
        XcoMSMQTransportService comm2;
        private ReceivedConsoleOut recvConsole;
        private ReceivedSendBack recvSendBack;
        [Test]
        public void TestPerformance()
        {
			XcoLoggerFactory.CreateInstance = (t => new TestLogger.TestLogger());

            comm1 = new XcoMSMQTransportService(msmqName1, true);
            comm2 = new XcoMSMQTransportService(msmqName2, true);
       

            try
            {
                recvConsole = new ReceivedConsoleOut();
                recvSendBack = new ReceivedSendBack(comm2,msmqName1);

                comm1.OnMessageReceived += recvConsole.MessageReceived;
				comm2.OnMessageReceived += recvSendBack.MessageReceived;
				comm1.Start();
                comm2.Start();

                byte[] testBytes = serializer.Serialize(new IntMsg { value = 0, strValue1 = 0.ToString(), strValue2 = "test", msgPartList = new List<MsgPart> { new MsgPart { exValue = new Exception(), strValue = "bla" } } });
                Console.WriteLine("Approximate Message Length: " + testBytes.Length);


                startedAt = DateTime.Now;
                recvSendBack.StartedAt = DateTime.Now;
                const int n = 5000;
                for (int i = 0; i <= n; i++)
                {

                    byte[] data = serializer.Serialize(new IntMsg
                                   {

                                       value = i,
                                       strValue1 = i.ToString(),
                                       strValue2 = "test",
                                       msgPartList =
                                           new List<MsgPart>
                                               {
                                                   new MsgPart
                                                       {
                                                           exValue = new Exception(),
                                                           strValue = "bla"
                                                       }
                                               }
                                   });

					comm1.Send(new XcoMessage { Content = data }, msmqName2);
                }
                Console.WriteLine("Send Finished: " + DateTime.Now.Subtract(startedAt).TotalSeconds);
                Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex);

            }
            finally
            {
                comm1.Stop();
                comm2.Stop();
            }

        }

      

       
    }
}